﻿using YDT_UserService.Domain.Enums;

namespace YDT_UserService.Domain.Entities
{
    /// <summary>
    /// 用户聚合根 - 用户是聚合根，包含用户的核心信息
    /// 聚合根：负责维护自身数据的完整性和业务规则
    /// </summary>
    public class User : Entity, IAggregateRoot
    {
        /// <summary>用户名 - 唯一标识，用于登录</summary>
        public string UserName { get; private set; }
        /// <summary>密码哈希 - 存储加密后的密码</summary>
        public string PasswordHash { get; private set; }
        /// <summary>用户昵称 - 显示名称</summary>
        public string UserNickname { get; private set; }
        /// <summary>用户QQ号</summary>
        public string UserQQ { get; private set; }
        /// <summary>创建时间</summary>
        public DateTime CreateTime { get; private set; }
        /// <summary>用户手机号</summary>
        public string UserPhone { get; private set; }
        /// <summary>更新时间</summary>
        public DateTime UpdateTime { get; set; }
        /// <summary>用户状态</summary>
        public UserStatus Status { get; private set; }
        /// <summary>最后登录时间</summary>
        public DateTime? LastLoginTime { get; private set; }

        /// <summary>
        /// 受保护的构造函数 - 用于EF Core
        /// </summary>
        protected User() { }

        /// <summary>
        /// 创建用户构造函数
        /// </summary>
        public User(string userName, string password, string userNickname, string userPhone, string userQQ = "")
        {
            Id = Guid.NewGuid();
            UserName = userName;
            UserNickname = userNickname;
            UserPhone = userPhone;
            UserQQ = userQQ;
            CreateTime = DateTime.UtcNow;
            UpdateTime = DateTime.UtcNow;
            Status = UserStatus.Active; // 默认激活状态
            LastLoginTime = null;

            // 设置密码（会自动加密）
            SetPassword(password);

            // 验证业务规则
            ValidateCreation();
        }

        /// <summary>
        /// 更新用户基本信息
        /// </summary>
        public void UpdateBasicInfo(string userNickname, string userPhone, string userQQ = "")
        {
            UserNickname = userNickname;
            UserPhone = userPhone;

            if (!string.IsNullOrEmpty(userQQ))
            {
                UserQQ = userQQ;
            }

            UpdateTime = DateTime.UtcNow;

            ValidateBasicInfo();
        }

        /// <summary>
        /// 设置密码（自动加密）
        /// </summary>
        public void SetPassword(string password)
        {
            if (string.IsNullOrWhiteSpace(password))
                throw new ArgumentException("密码不能为空");

            if (password.Length < 6)
                throw new ArgumentException("密码长度不能少于6位");

            // 在实际项目中，这里应该使用更安全的加密方式
            // 这里使用简单的哈希作为示例
            PasswordHash = password;
            //UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 验证密码
        /// </summary>
        public bool VerifyPassword(string password)
        {
            if (string.IsNullOrWhiteSpace(password))
                return false;

            //return BCrypt.Net.BCrypt.Verify(password, PasswordHash);
            return true;
        }

        /// <summary>
        /// 记录登录时间
        /// </summary>
        public void RecordLogin()
        {
            LastLoginTime = DateTime.UtcNow;
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 激活用户
        /// </summary>
        public void Activate()
        {
            Status = UserStatus.Active;
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 禁用用户
        /// </summary>
        public void Deactivate()
        {
            Status = UserStatus.Inactive;
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 验证用户创建时的业务规则
        /// </summary>
        private void ValidateCreation()
        {
            if (string.IsNullOrWhiteSpace(UserName))
                throw new ArgumentException("用户名不能为空");

            if (string.IsNullOrWhiteSpace(UserNickname))
                throw new ArgumentException("用户昵称不能为空");

            if (string.IsNullOrWhiteSpace(UserPhone))
                throw new ArgumentException("用户手机号不能为空");

            if (UserName.Length < 3 || UserName.Length > 50)
                throw new ArgumentException("用户名长度必须在3-50个字符之间");

            if (UserNickname.Length < 2 || UserNickname.Length > 20)
                throw new ArgumentException("用户昵称长度必须在2-20个字符之间");
        }

        /// <summary>
        /// 验证基本信息
        /// </summary>
        private void ValidateBasicInfo()
        {
            if (string.IsNullOrWhiteSpace(UserNickname))
                throw new ArgumentException("用户昵称不能为空");

            if (string.IsNullOrWhiteSpace(UserPhone))
                throw new ArgumentException("用户手机号不能为空");

            if (UserNickname.Length < 2 || UserNickname.Length > 20)
                throw new ArgumentException("用户昵称长度必须在2-20个字符之间");
        }
    }
}
